package com.yh.homestay.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.UserOwnerMapper;
import com.yh.homestay.pojo.UserOwner;
import com.yh.homestay.service.UserOwnerService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class UserOwnerServiceImpl implements UserOwnerService {

    private Log logger = LogFactory.getLog(UserOwnerServiceImpl.class);

    @Autowired
    private UserOwnerMapper userOwnerMapper;

    /**
    * @Date: 2020/1/13 14:09
    * @Description: 查询出所有用户信息
    * @param ownerName
     * @param page
     * @param limit
    * @return: com.yh.homestay.common.HomeStayResultG
    */
    @Override
    public HomeStayResult showUserOwner(String ownerName, Integer page, Integer limit) {
        try {
            PageHelper.startPage(page, limit);
            List<UserOwner> userOwners;
            if (StringUtils.isEmpty(ownerName)) {
                userOwners = userOwnerMapper.selectAll();
            } else {
                Example example = new Example(UserOwner.class);
                example.createCriteria().andLike("ownerName", "%" + ownerName + "%");
                userOwners = userOwnerMapper.selectByExample(example);
            }
            PageInfo info = new PageInfo(userOwners);
            return new HomeStayResult(0, "ok", (int) info.getTotal(), info.getList());
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
    * @Date: 2020/1/13 14:09
    * @Description: 插入用户信息
    * @param userOwner
    * @return: com.yh.homestay.common.HomeStayResultG
    */
    @Override
    public HomeStayResult insertUserOwner(UserOwner userOwner) {
        try{
            userOwnerMapper.insertSelective(userOwner);
            return HomeStayResult.ok();
        }catch (Exception e){
            logger.debug(e);
            return HomeStayResult.build(500,"error");
        }
    }

    /**
    * @Date: 2020/1/13 14:09
    * @Description: 删除用户信息
    * @param id
    * @return: com.yh.homestay.common.HomeStayResultG
    */
    @Override
    public HomeStayResult deleteUserOwner(Integer id) {
       try{
           userOwnerMapper.deleteByPrimaryKey(id);
           return HomeStayResult.ok();
       }catch (Exception e){
           logger.debug(e);
           return HomeStayResult.build(500,"error");
       }
    }

    /**
    * @Date: 2020/1/13 14:09
    * @Description: 更新用户信息
    * @param userOwner
    * @return: com.yh.homestay.common.HomeStayResultG
    */
    @Override
    public HomeStayResult updateUserOwner(UserOwner userOwner) {
       try{
           userOwnerMapper.updateByPrimaryKeySelective(userOwner);
           return HomeStayResult.ok();
       }catch (Exception e){
           logger.debug(e);
           return HomeStayResult.build(500,"error");
       }
    }

    /**
    * @Date: 2020/1/13 14:09
    * @Description: 查询出一个用户信息
    * @param id
    * @return: com.yh.homestay.common.HomeStayResultG
    */
    @Override
    public HomeStayResult selectOneUserOwner(Integer id) {
       try{
           UserOwner userOwner = userOwnerMapper.selectByPrimaryKey(id);
           return HomeStayResult.ok(200,userOwner);
       }catch (Exception e){
           logger.debug(e);
           return HomeStayResult.build(500,"error");
       }
    }

    /**
    * @Date: 2020/1/13 14:09
    * @Description: 批量删除用户信息
    * @param ids
    * @return: com.yh.homestay.common.HomeStayResultG
    */
    @Override
    public HomeStayResult deleteBatchUserOwner(List<Integer> ids) {
        try{
            for (Integer id:ids) {
                userOwnerMapper.deleteByPrimaryKey(id);
            }
            return HomeStayResult.ok();
        }catch (Exception e){
            logger.debug(e);
            return HomeStayResult.build(500,"error");
        }
    }


}
